﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ABELGX
{
    /// <summary>
    /// WindowParameter.xaml 的交互逻辑
    /// </summary>
    public partial class WindowParameter : Window
    {
        Process Winvirkey;
        AdjustScreenByWmi changeLight = new AdjustScreenByWmi();
        UserSet UserSet;
        List<UserControlEditItem> editItems;
        public WindowParameter()
        {
            InitializeComponent();

            editItems = new List<UserControlEditItem>
            {
                Param1,
                Param2,
                Param3,
                Param4,
                Param5,
                Param6,
            };

            Winvirkey = Process.Start("osk.exe");
            Winvirkey.Kill();
        }
        public void SetInfo(List<SensorSet> sets, UserSet userSet)
        {
            UserSet=userSet;
            for (int i = 0; i < editItems.Count; i++)
            {
                var set0 = sets.Find(x => x.Index == (i + 1));
                if(set0 != null)
                {
                    editItems[i].Visibility = Visibility.Visible;
                    if(userSet.Params.Count<=i)
                    {
                        editItems[i].SetInfo(set0, 0);
                    }
                    else
                    {
                        editItems[i].SetInfo(set0, userSet.Params[i]);
                    }
                    
                }
                else
                {
                    editItems[i].Visibility = Visibility.Hidden;
                }
            }
            SoundSliderBar.Value = Config.Volume.Current;
            SoundValue.Content = SoundSliderBar.Value.ToString() + "%";
            LightSliderBar.Value = changeLight.GetBrightness();
            LightValue.Content = LightSliderBar.Value.ToString() + "%";
        }

        private void LightSliderBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                changeLight.StartupBrightness((int)LightSliderBar.Value);
                LightValue.Content = LightSliderBar.Value.ToString("f0") + "%";
            }
            catch
            {

            }
        }

        private void SoundSliderBar_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            SoundValue.Content = SoundSliderBar.Value.ToString("f0") + "%";
            Config.Volume.SetVolume((int)SoundSliderBar.Value);
        }

        private void SoftKey_Click(object sender, RoutedEventArgs e)
        {
            if (Winvirkey.HasExited)
            {
                Winvirkey = Process.Start("osk.exe");
            }
            else
            {
                Winvirkey.Kill();
            }
        }

        private void ButtonOk_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < editItems.Count; i++)
            {
                if(UserSet.Params.Count<=i)
                {
                    UserSet.Params.Add(0);
                }
                UserSet.Params[i] = editItems[i].Value;
            }
            Config.SaveUserSet();
            Hide();
        }

        private void ButtonCancel_Click(object sender, RoutedEventArgs e)
        {
            Hide();
        }
    }
}
